<template>
    <TCollapse title="财务成本数据">
        <el-row :gutter="10" class="marginBottom14" type="flex" justify="start">
            <el-col :span="1.5" style="color:red">
                <p style="margin-bottom: 16px;">(1) 请先更新财务数据，获取财务最新转资结果</p>
                <p>(2) 如更新后竣工决算金额为0元，请到财务系统核实是否完成转资</p>
            </el-col>
        </el-row>
        <el-row :gutter="10" class="marginBottom14" type="flex" justify="end" v-if="showBtn && sendSuccess">
            <el-col :span="1.5">
                <el-button type="primary" class="pms-button-primary" @click="updateFinance">更新财务数据</el-button>
            </el-col>
        </el-row>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane v-for="item in tabsOption" :key="item.name" :label="item.label" :name="item.name">
                <FinalAccountsTable :tableData="tableData" :columns="columns" :colOptionEnum="getdicLists" />
                <pagination v-if="pageParam.total > 10" :total="pageParam.total" :page.sync="pageParam.pageNum"
                    :limit.sync="pageParam.pageSize" @pagination="doSearch" />
                <el-row style="margin-top: 20px;" :gutter="10" class="marginBottom14" type="flex" justify="center"
                    v-if="showBtn && sendSuccess&&getAuditInfo.isPush!=='02'">
                    <el-col :span="1.5">
                        <el-button type="primary" class="pms-button-primary" @click="projectReview">项目送审</el-button>
                    </el-col>
                </el-row>
            </el-tab-pane>
        </el-tabs>
    </TCollapse>
</template>

<script>
import FinalAccountsTable from '@/views/finalAccounts/components/finalAccountsTable.vue'
import SysConst from '@/common/const'
import { tabsOption, tabsColumnsOption } from '@/views/finalAccounts/setting'
import {
    queryTSubject,
    queryTClosingPrjB,
    queryTClosingPrjC,
    queryTClosingPrjD,
    queryTClosingPrjE,
    queryTClosingPrjF,
    queryTClosingPrjG,
    finalSentAudit,
    updateFinancialData
} from '@/api/finalAccounts/index'
import { mapGetters } from 'vuex'
export default {
    name: 'GlobalTabs',
    components: {
        FinalAccountsTable
    },
    inject: ['prjDetail'],
    data() {
        return {
            activeName: 'delivery',
            tabsOption,
            tabsColumnsOption,
            columns: [],
            tableData: [],
            pageParam: {
                pageSize: SysConst.PAGESIZE, // 每页显示val条
                pageNum: SysConst.CURRENT_PAGE, // 当前页
                total: SysConst.TOTAL// 总数
            },
            searchParam: {
                pageSize: SysConst.PAGESIZE, // 每页显示val条
                pageNum: SysConst.CURRENT_PAGE, // 当前页
            },
            sendSuccess: true
        }
    },
    watch: {
        'getAuditInfo.faId': {
            handler(val) {
                if (val) {
                    this.getInitColumnsInfo()
                }
            }
        }
    },
    computed: {
        ...mapGetters('finalAccounts', ['getAuditInfo', 'getdicLists']),

        showBtn() {
            return +this.$route.query.status === 1
        },
        newPrjDetail() {
            return this.prjDetail()
        },
    },
    methods: {
        async updateFinance() {
            try {
                const params = {
                    tfaId: this.getAuditInfo.faId,
                    operation: 1
                }
                const { code } = await updateFinancialData(params)
                if (+code === 200) {
                    this.$message.success('更新成功')
                }
            } catch (error) {
                console.log(error);
            }
        },
        async projectReview() {
            try {
                const params = {
                    tfaId: this.getAuditInfo.faId,
                    isInertHistory: 1
                }
                const { code, message } = await finalSentAudit(params)
                if (+code === 200) {
                    this.sendSuccess = false
                    this.$message.success(message)
                }
            } catch (error) {
                console.log(error);
            }
        },
        getInitColumnsInfo() {
            this.columns = this.tabsColumnsOption[this.activeName]
            this[this.activeName]()
        },
        handleClick(tab, event) {
            this.tableData = []
            this.getInitColumnsInfo()
        },
        backParams() {
            return `?faId=${this.getAuditInfo?.faId}&pageNum=${this.searchParam.pageNum}&pageSize=${this.searchParam.pageSize}`
        },
        async delivery() {
            try {
                const { data } = await queryTSubject(this.backParams())
                this.tableData = data
            } catch (error) {
                console.log(error);
            }
        },
        async auxiliary() {
            try {
                const { data } = await queryTClosingPrjB(this.backParams())
                this.tableData = data
            } catch (error) {
                console.log(error);
            }
        },
        async paymentPrice() {
            try {
                const { data } = await queryTClosingPrjC(this.backParams())
                this.tableData = data
            } catch (error) {
                console.log(error);
            }
        },
        async paymentInfo() {
            try {
                const { data } = await queryTClosingPrjD(this.backParams())
                this.tableData = data.list
            } catch (error) {
                console.log(error);
            }
        },
        async priceCost() {
            try {
                const { data } = await queryTClosingPrjE(this.backParams())
                this.tableData = data.list
            } catch (error) {
                console.log(error);
            }
        },
        async deviceCost() {
            try {
                const { data } = await queryTClosingPrjF(this.backParams())
                this.tableData = data.list
            } catch (error) {
                console.log(error);
            }
        },
        async costSheetResult() {
            try {
                const { data } = await queryTClosingPrjG(this.backParams())
                this.tableData = data.list
            } catch (error) {
                console.log(error);
            }
        },
        // 分页
        async doSearch(item) {
            this.searchParam = {
                pageNum: item?.page || SysConst.CURRENT_PAGE,
                pageSize: item?.limit || SysConst.PAGESIZE,
            }
            this[this.activeName]()
        }
    },
}

</script>
<style scoped>
::v-deep .el-tabs__header {
    margin: 0 0 0 0;
}
</style>